$(function () {
    //抽取js变量
    var mission_datagrid = $("#mission_datagrid");
    var mission_dialog = $("#mission_dialog");
    var mission_form = $("#mission_form");
    var button_check = $("#lookover");

    $("#box").on('click', 'a.my_btn', function () {
        var index = $(this).data("index");
        var rows = mission_datagrid.datagrid("getRows");
        var row = rows[index];
        mission_dialog.dialog("open");
        mission_dialog.dialog("setTitle", "查看任务");
        mission_form.form("load", row);
        $("#mission_dialog input").textbox({
            readonly: true
        });
        $("#textarea").prop("disabled", true);
        $("#save1").prop("disabled", true);

    });
    mission_datagrid.datagrid({
        url: '/mission/query.do',
        fitColumns: true,
        fit: true,//自动填充
        toolbar: '#mission_toolbar',
        pagination: true, //在DataGrid控件底部显示分页工具栏
        rownumbers: true, //显示一个行号列。
        singleSelect: true,//只允许选择一行
        striped: true, //显示斑马线效果
        columns: [[
            {field: 'id', checkbox: true},
            {field: 'date', title: '任务建立日期', width: 80},
            {field: 'employeeuserame', title: '员工用户名', width: 80},
            {field: 'deptname', title: '所属部门', width: 80},
            {field: 'sn', title: '任务编号', width: 80},
            {
                field: 'state', title: '状态', width: 80, formatter: function (value, row, index) {
                    return value ? "<font color='green'>已完成</font>" : "<font color='orange'>待完成</font>";
                }
            },
            {
                field: 'button', title: '查看任务', width: 80, formatter: function (value, row, index) {
                    return "<a href='javascript:' data-index='" + index + "' class='easyui-linkbutton my_btn'>查看</a>";
                }
            }
        ]],
        onSelect: function (index, row) {

            $("#button_edit").linkbutton({
                disabled: row.state
            });
            $("#button_mark").linkbutton({
                disabled: row.state
            });
        }
    });

    $("#mission_toolbar a").click(function () {
        var keyword1 = $("#keyword").textbox('getValue');
        var deptName1 = $("#deptName").combobox('getValue');
        var beginDate1 = $("#beginDate").datebox('getValue');
        var endDate1 = $("#endDate").datebox('getValue');

        mission_datagrid.datagrid('load', {
            keyword: keyword1,
            deptName: deptName1,
            beginDate: beginDate1,
            endDate: endDate1
        });
    });

    var methodObj = {
        search: function () {
            var keyword1 = $("#keyword").textbox('getValue');
            var deptName1 = $("#deptName").combobox('getValue');
            var beginDate1 = $("#beginDate").datebox('getValue');
            var endDate1 = $("#endDate").datebox('getValue');
            mission_datagrid.datagrid('load', {
                keyword: keyword1,
                deptName: deptName1,
                beginDate: beginDate1,
                endDate: endDate1
            });
        },
        add: function () {
            mission_dialog.dialog("open");
            mission_dialog.dialog("setTitle", "新建任务");
            $("#mission_dialog input").textbox({
                readonly: false
            });
            $("#textarea").prop("disabled", false);
            $("#save1").prop("disabled", false);
        },
        edit: function () {
            var row = mission_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '亲,请选择一条数据', 'warning')
                return
            }
            mission_dialog.dialog("open");
            mission_dialog.dialog("setTitle", "编辑任务");
            mission_form.form("load", row);
            $("#mission_dialog input").textbox({
                readonly: false
            });
            $("#textarea").prop("disabled", false);
            $("#save1").prop("disabled", false);

        },
        mark: function () {
            var row = mission_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '亲,请选择一条数据', 'warning')
                return
            }
            $.get('/mission/changeState.do', {state: true, id: row.id}, function (data) {
                if (data.success) {
                    $.messager.alert("老板就是喜欢能干的员工", '标记成功', 'info', function () {
                        mission_datagrid.datagrid("reload");
                    });
                } else {
                    $.messager.alert("温馨提示", "标记失败", "error");
                }
            });

        },
        refresh: function () {
            mission_datagrid.datagrid("reload");
            $.messager.alert("温馨提示", "刷新完毕,赶快接单吧亲!", "info");
        },
        printing: function () {
            $.messager.alert("温馨提示", "打印成功:文件已保存至D/ideaSource/crm/print.txt", "info");
        },

        save: function () {
            mission_form.form('submit', {

                url: '/mission/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '操作成功!', 'info', function () {
                            methodObj.cancel();
                            mission_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', '操作失败!', 'error');
                    }
                }
            });

        },
        cancel: function () {
            mission_dialog.dialog("close");
        }

    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    });

    mission_dialog.dialog({
        width: 320,
        height: 420,
        top: 100,
        buttons: "#mission_btns",
        closed: true,
        onClose: function () {
            mission_form.form('clear');
        }
    });


});










